package com.hujing.mq.consumer;

import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;


@Component
@Slf4j
public class MessageConsumer {
    
    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue(value = "test"),
                    exchange = @Exchange(value = "testExchange"),
                    key="234"
            )
    })
    public void receive(Channel channel, Message message, @Payload String payload) throws Exception {
        try {
            Thread.sleep(3000);
            log.info("Received message: {}", payload);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), true);
        }
    }
    
    @RabbitListener(bindings = {@QueueBinding(value = @Queue(value = "test2"), exchange=@Exchange(value = "testExchange"),key = {"234","567"})})
    public void receive2(Channel channel,Message message,String msg)throws Exception{
        try {
            System.out.println("接收到新的消息："+msg);
        } finally {
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        }
    }
}
